package vn.bk.hotel;

import java.util.ArrayList;

import com.actionbarsherlock.app.SherlockFragment;

import vn.bk.hotel.global.Language;
import vn.bk.hotel.global.Service;
import hust.sig.api.service.model.SimpleDataObject;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ChooseDistrict extends SherlockFragment {

    public static int nameCity = 0;
    private String[] uriCity;
    private ListView listDistrict;
    private View rootView;
    private ArrayList<String> arrayDistrict;
    private SimpleDataObject[] simpleDataObjects;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
	    Bundle savedInstanceState) {
	rootView = inflater.inflate(R.layout.choosedistrict, container, false);
	uriCity = getResources().getStringArray(R.array.city_uri);
	init();
	return rootView;
    }

    private void init() {
	listDistrict = (ListView) rootView.findViewById(R.id.listDistrict);

	arrayDistrict = new ArrayList<String>();
	GetListDistrict getListDistrict = new GetListDistrict(getActivity());
	getListDistrict.execute("finding");
	listDistrict.setOnItemClickListener(new ListView.OnItemClickListener() {

	    @Override
	    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
		    long arg3) {
		// TODO Auto-generated method stub
		ListHotel.dataObject = simpleDataObjects[position];
		ListHotel.typeSearch = ListHotel.SEARCH_BY_DISTRICT;
		ListHotel.uriType = "http://hust.se.vtio.owl#Accomodation";
		getActivity().startActivity(new Intent(getActivity(), ListHotel.class));
	    }
	});
    }

    public class GetListDistrict extends AsyncTask<String, Integer, String> {

	private ProgressDialog dialog;
	private Context mContext;
	private boolean find = true;

	public GetListDistrict(Context context) {
	    this.mContext = context;
	}

	@Override
	protected void onPreExecute() {
	    // TODO Auto-generated method stub
	    super.onPreExecute();
	    this.dialog = new ProgressDialog(mContext);
	    this.dialog.setMessage("Loading.....");
	    this.dialog.show();
	    this.dialog.setCancelable(false);

	}

	@SuppressWarnings("deprecation")
	@Override
	protected String doInBackground(String... params) {

	    try {
		simpleDataObjects = Service.getHotelService()
			.getAllDistrictOfCity(uriCity[nameCity],
				Language.getLanguage(mContext));

	    } catch (Exception e) {

		find = false;
	    }
	    return null;
	}

	@Override
	protected void onPostExecute(String result) {
	    // TODO Auto-generated method stub
	    super.onPostExecute(result);
	    this.dialog.dismiss();
	    if (find) {
		for (int i = 0; i < simpleDataObjects.length; i++) {
		    arrayDistrict.add(simpleDataObjects[i].getLabelString()
			    .replace("@vn", "").replace("@en", ""));
		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(
			mContext, android.R.layout.simple_list_item_1,
			arrayDistrict);
		listDistrict.setAdapter(adapter);
	    } else {
		Toast.makeText(mContext,  getString(R.string.error_connect),
			Toast.LENGTH_SHORT).show();
	    }

	}

    }

}
